package com.kevin.ymqquan.activity;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.DisplayMetrics;
import android.widget.ImageView;

import com.kevin.ymqquan.MainActivity;
import com.kevin.ymqquan.R;
import com.kevin.ymqquan.application.App;

public class WelcomeActivity extends Activity {
	boolean isFirstIn = false;

	private boolean isLogin = false;
	private int userId;
	private int userType;

	private static final int GO_HOME = 1000;
	private static final int GO_GUIDE = 1001;
	// 延迟3秒
	private static final long SPLASH_DELAY_MILLIS = 3000;

	private static final String SHAREDPREFERENCES_NAME = "first_pref";
	private static final String LOGIN = "login_pref";

	private App application;

	private ImageView iv_welcome_image;
	private ByteArrayOutputStream outputStream;

	private ByteArrayInputStream bAiS;

	/**
	 * Handler:跳转到不同界面
	 */
	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case GO_HOME:
				if (application.userId == 0) {
					goLogin();
				} else {
					// goLogin();
					goHome();
				}
				break;
			case GO_GUIDE:
				try {
					// goGuide();
					goHome();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcome);

		iv_welcome_image = (ImageView) findViewById(R.id.iv_welcome_image);
		iv_welcome_image.setBackgroundResource(R.drawable.welcome_android3);
		getMetrics();
		init();
	}

	private void init() {
		// 读取SharedPreferences中需要的数据
		// 使用SharedPreferences来记录程序的使用次数
		// SharedPreferences preferences = getSharedPreferences(
		// SHAREDPREFERENCES_NAME, MODE_PRIVATE);

		application = (App) this.getApplication();

		// 取得相应的值，如果没有该值，说明还未写入，用true作为默认值
		// isFirstIn = preferences.getBoolean("isFirstIn", true);

		// 判断程序与第几次运行，如果是第一次运行则跳转到引导界面，否则跳转到主界面
		// if (!isFirstIn) {
		// 使用Handler的postDelayed方法，3秒后执行跳转到MainActivity
		// !!真正使用时取消注释,使导航页面只运行一次
		mHandler.sendEmptyMessageDelayed(GO_HOME, SPLASH_DELAY_MILLIS);
		// mHandler.sendEmptyMessageDelayed(GO_GUIDE, SPLASH_DELAY_MILLIS);
		// } else {
		// mHandler.sendEmptyMessageDelayed(GO_GUIDE, SPLASH_DELAY_MILLIS);
		// }

		SharedPreferences loginPreferences = getSharedPreferences(LOGIN,
				MODE_PRIVATE);
		String getPreferencesId = loginPreferences.getString("userId", "0");
		if ("".equals(getPreferencesId)) {
			getPreferencesId = "0";
		}
		userId = Integer.parseInt(getPreferencesId);
		application.userId = userId;

	}

	private void goHome() {
		Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
		WelcomeActivity.this.startActivity(intent);
		WelcomeActivity.this.finish();
	}

	private void goLogin() {
		Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);
		WelcomeActivity.this.startActivity(intent);
		WelcomeActivity.this.finish();
	}

	//
	// private void goGuide() {
	// Intent intent = new Intent(WelcomeActivity.this, GuideActivity.class);
	// WelcomeActivity.this.startActivity(intent);
	// WelcomeActivity.this.finish();
	// }

	/**
	 * 获取屏幕分辨率
	 */
	private void getMetrics() {
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);
		int widthPixels = metrics.widthPixels;
		int heightPixels = metrics.heightPixels;
		final App app = (App) getApplication();
		app.setHeightPixels(heightPixels);
		app.setWidthPixels(widthPixels);
		// ShowUtil.ShowMessage(WelcomeActivity.this,
		// heightPixels+"*"+widthPixels);
	}
}
